package com.module02.demo02;

import java.util.Scanner;

public class GobangGame {

    Scanner sc = new Scanner(System.in);
    //创建char类型的二位数组用于保存字符
    private final String[][] BOARDARR = new String[18][18];

    //绘制棋盘
    private void drawBoard() {
        //利用双重for循环给二维数组赋值
        for (int i = 0; i < BOARDARR.length; i++) {
            for (int j = 0; j < BOARDARR[i].length; j++) {
                //给第一行赋值
                if (i == 0) {
                    if (j == 0) {
                        BOARDARR[0][0] = " ";
                    } else if (j <= 10) {
                        BOARDARR[i][j] = (char) ('/' + j) + "";//依次赋值0-9
                    } else {
                        BOARDARR[i][j] = (char) ('V' + j) + "";//依次赋值a-f
                    }
                }
                //给第一列赋值
                if (j == 0) {
                    if (i == 0) {
                        BOARDARR[0][0] = " ";
                    } else if (i <= 10) {
                        BOARDARR[i][j] = (char) ('/' + i) + "";//依次赋值0-9
                    } else {
                        BOARDARR[i][j] = (char) ('V' + i) + "";//依次赋值a-f
                    }
                }
                //给第18行18列赋值
                if (i >= 17 || j >= 17) {
                    BOARDARR[i][j] = " ";
                }

                //给其他行列赋值
                if (i > 0 && i < 17 && j > 0 && j < 17) {
                    BOARDARR[i][j] = "+";
                }
                System.out.print("  " + BOARDARR[i][j]);
            }
            System.out.println();
        }
    }


    //定义棋子颜色和黑白方的常量
    private static final String BLACK_CHESS = "●";
    private static final String WHITE_CHESS = "○";
    private static final String PLAYER1 = "黑色方";
    private static final String PLAYER2 = "白色方";
    //接受用户传进来的坐标值
    private int x;
    private int y;
    //设置标签
    private boolean flag = true;

    //提示下棋并重新绘制
    private void beat() {
        System.out.println("请" + (flag ? PLAYER1 : PLAYER2) + "下子，输入格式为:x y");

        x = (int) sc.next().charAt(0);
        if (x <= 70) {
            x = x - 47;
        } else {
            x = x - 86;
        }
        y = (int) sc.next().charAt(0);
        if (y <= 70) {
            y = y - 47;
        } else {
            y = y - 86;
        }

        //判断输入是否有效，或者重复落子
        if ((x < 0 || x > 16 || y < 0 || y > 16) || (!BOARDARR[x][y].equals("+"))) {
            System.out.println("无效的位置,请重新输入");
            beat();//坐标过界或重复，重新输入
        } else {//赋值 并重新绘制棋盘
            for (int i = 0; i < BOARDARR.length; i++) {
                for (int j = 0; j < BOARDARR[i].length; j++) {
                    if (flag) {
                        BOARDARR[x][y] = BLACK_CHESS;
                    } else {
                        BOARDARR[x][y] = WHITE_CHESS;
                    }
                    System.out.print("  " + BOARDARR[i][j]);
                }
                System.out.println();
            }
            flag = !flag;
        }


    }

    //判断胜负
    public void startGame() throws InterruptedException {

        System.out.println("游戏加载中 > >");
        for (int i = 1; i <= 10; i++) {
            Thread.sleep(500);
            System.out.print(" ● ");
        }
        System.out.println("> >加载完成");
        Thread.sleep(1000);
        System.out.println("==================================");
        System.out.println("           进入游戏[1]");
        System.out.println("           离开游戏[0]");
        System.out.println("==================================");

        int choose = sc.nextInt();
        if (choose == 1) {


            System.out.println();
            System.out.println("  ==================欢迎来到五子棋游戏===================");
            System.out.println("                        作者：WHX                     ");
            System.out.println("                     黑方先下，落棋无悔！                 ");
            System.out.println("  ====================================================");
            drawBoard();

            //棋子连续的个数
            int cnt = 1;
            while (cnt < 5) {

                beat();

                //判断棋子颜色
                String chessType = BOARDARR[x][y];
                int a = 1;
                while (a <= 4) {

                    //横向五子连线判断
                    if (a == 1) {
                        //向右判断
                        for (int i = 1; i < 5; i++) {
                            if (BOARDARR[x][y + i].equals(chessType)) {
                                cnt++;
                            } else {
                                break;
                            }
                        }
                        //向左判断
                        for (int i = 1; i < 5; i++) {
                            if (BOARDARR[x][y - i].equals(chessType)) {
                                cnt++;
                            } else {
                                break;
                            }
                        }
                    }

                    //纵向五子连线判断
                    if (a == 2) {
                        for (int i = 1; i < 5; i++) {
                            if (BOARDARR[x + i][y].equals(chessType)) {
                                cnt++;
                            } else {
                                break;
                            }
                        }

                        //向上判断
                        for (int i = 1; i < 5; i++) {
                            if (BOARDARR[x - i][y].equals(chessType)) {
                                cnt++;
                            } else {
                                break;
                            }
                        }
                    }

                    //左上到右下五子连线判断
                    if (a == 3) {
                        for (int i = 1; i < 5; i++) {
                            if (BOARDARR[x + i][y + i].equals(chessType)) {
                                cnt++;
                            } else {
                                break;
                            }
                        }
                        //向左上判断
                        for (int i = 1; i < 5; i++) {
                            if (BOARDARR[x - i][y - i].equals(chessType)) {
                                cnt++;
                            } else {
                                break;
                            }
                        }

                    }

                    //左下到右上纵向五子连线判断
                    if (a == 4) {
                        for (int i = 1; i < 5; i++) {
                            if (BOARDARR[x - i][y + i].equals(chessType)) {
                                cnt++;
                            } else {
                                break;
                            }
                        }
                        //向左下判断
                        for (int i = 1; i < 5; i++) {
                            if (BOARDARR[x + i][y - i].equals(chessType)) {
                                cnt++;
                            } else {
                                break;
                            }
                        }

                    }

                    //判断哪一方获胜
                    if (cnt >= 5) {
                        System.out.println("Game Over !");
                        if (BOARDARR[x][y].equals(BLACK_CHESS)) {
                            System.out.println(PLAYER1 + "获胜 ！");
                        } else {
                            System.out.println(PLAYER2 + "获胜 ！");
                        }
                        a = 5;
                    } else {
                        cnt = 1;
                    }
                    a++;
                }
            }
        } else {
            System.out.println("离开游戏");
        }
    }

}
